//
//  WFViewController.m
//  iRSSNews
//
//  Created by TrongTuan on 12/31/12.
//  Copyright (c) 2012 Mobioneer Ltd. All rights reserved.
//

#import "WFViewController.h"

@interface WFViewController ()

@end

@implementation WFViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [super viewDidLoad];
    self.title = @"Tuoi Tre";
    self.list = [NSMutableArray array];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionSheet)];
    NSURL *url = [NSURL URLWithString:self.newweb.link];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.webview  loadRequest:requestObj];

}
-(void)actionSheet{
    UIActionSheet *alert = [[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:NSLocalizedString(@"CancelKey", @"") destructiveButtonTitle:NSLocalizedString(@"ShareMailKey", @"") otherButtonTitles:NSLocalizedString(@"ShareFacebookKey", @""),NSLocalizedString(@"ShareTwitterKey", @""), nil];
    [alert showInView:[UIApplication sharedApplication].keyWindow];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
        mailComposer.mailComposeDelegate = self;
        NSArray *emailAddresses = [[NSArray alloc] initWithObjects:@"", nil];
        NSString *sendSubject = [[NSString alloc] initWithFormat:NSLocalizedString(@"ShareNewsKey", @""),self.newweb.title];
        NSString *sendMessage = [[NSString alloc] initWithFormat:NSLocalizedString(@"AlertShareMailKey", @""),self.newweb.link];
        [mailComposer setToRecipients:emailAddresses];
        [mailComposer setSubject:sendSubject];
        [mailComposer setMessageBody:sendMessage isHTML:YES];
        [self presentViewController:mailComposer animated:YES completion:nil];
    }
    if (buttonIndex == 2) {
        TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
        NSString *link = [NSString stringWithFormat:NSLocalizedString(@"AlertShareTwitterKey", @""),self.newweb.link] ;
        [twitter setInitialText:link];
        [twitter addImage:[UIImage imageNamed:@"image.png"]];
        
        [self presentViewController:twitter animated:YES completion:nil];
        
        twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
            
            if(res == TWTweetComposeViewControllerResultDone) {
                
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"SuccessKey", @"") message:NSLocalizedString(@"AlertShareTwitterOKKey", @"") delegate:self cancelButtonTitle:NSLocalizedString(@"DismissKey", @"") otherButtonTitles: nil];
                
                [alert show];
                
            }   
        };
    }
}
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    switch (result) {
        case MFMailComposeResultCancelled:
            break;
        case MFMailComposeResultSaved:
            break;
        case MFMailComposeResultSent:
            break;
        case MFMailComposeResultFailed:
            break;
            
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
